package com.ecnucs.shortlink.utils;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.json.JSONUtil;
import com.ecnucs.shortlink.common.Result;
import com.ecnucs.shortlink.common.ShortLinkException;
import com.ecnucs.shortlink.config.ShortLinkConfiguration;
import com.ecnucs.shortlink.dto.LinkCreateDTO;
import com.ecnucs.shortlink.dto.UserLoginDTO;
import com.ecnucs.shortlink.vo.GroupVO;
import com.ecnucs.shortlink.vo.LinkCreateVO;
import com.ecnucs.shortlink.vo.UserLoginVO;

import java.time.LocalDateTime;
import java.util.List;

public class ShortLinkTemplate {
    private static final Integer CREATED_TYPE = 0;
    private static final Integer VALID_DATE_TYPE = 0;
    private static final LocalDateTime VALID_DATE = null;
    private static final String DESCRIBE = "短链接: %s";

    private final ShortLinkConfiguration shortLinkConfiguration;
    private String token;

    public ShortLinkTemplate(ShortLinkConfiguration shortLinkConfiguration) throws ShortLinkException {
        this.shortLinkConfiguration = shortLinkConfiguration;
        token = login();
    }

    /**
     * 调用登录接口
     * @return 登录后获取的token
     */
    private String login() throws ShortLinkException {
        // 1. 创建登录参数
        UserLoginDTO userLoginDTO = UserLoginDTO.builder()
                .username(shortLinkConfiguration.getUsername())
                .password(shortLinkConfiguration.getPassword()).build();
        // 创建请求
        String url = "http://" + shortLinkConfiguration.getAdminHost() + ":" + shortLinkConfiguration.getAdminPort() + "/api/short-link/admin/v1/user/login";
        String json = JSONUtil.toJsonStr(userLoginDTO);
        String returnJson = HttpUtil.postData(url, json);

        // 使用TypeReference指定Result<UserLoginVO>的类型
        Result<UserLoginVO> result = JSONUtil.toBean(returnJson, new TypeReference<Result<UserLoginVO>>() {}, true);
        // 获取UserLoginVO对象
        UserLoginVO userLoginVO = result.getData();
        // 获取token
        return userLoginVO.getToken();
    }

    private Long getFirstGroupId() throws ShortLinkException {
        // 1. 检查是否登录
        if (!checkLogin()) {
            token = login();
        }
        String url ="http://" + shortLinkConfiguration.getAdminHost() + ":" + shortLinkConfiguration.getAdminPort() + "/api/short-link/admin/v1/group";
        String returnJson = HttpUtil.getData(url, token);
        // 使用TypeReference指定Result<UserLoginVO>的类型
        Result<List<GroupVO>> result = JSONUtil.toBean(returnJson, new TypeReference<Result<List<GroupVO>>>() {}, true);

        List<GroupVO> groupVOList = result.getData();
        return groupVOList.get(0).getGid();
    }

    public String createLink(String originUrl) throws ShortLinkException {
        Long gid = getFirstGroupId();
        return createLink(originUrl, gid);
    }
    public String createLink(String originUrl, Long gid) throws ShortLinkException {
        return createLink(originUrl, gid, DESCRIBE);
    }

    public String createLink(String originUrl, Long gid, String describe) throws ShortLinkException {
        // 1. 检查是否登录
        if (!checkLogin()) {
            token = login();
        }

        LinkCreateDTO linkCreateDTO = LinkCreateDTO.builder()
                .originUrl(originUrl)
                .gid(gid)
                .createdType(CREATED_TYPE)
                .validDateType(VALID_DATE_TYPE)
                .validDate(VALID_DATE)
                .describe(describe).build();

        String url ="http://" + shortLinkConfiguration.getLinkHost() + ":" + shortLinkConfiguration.getLinkPort() + "/api/short-link/v1/create";
        String json = JSONUtil.toJsonStr(linkCreateDTO);
        String returnJson = HttpUtil.postData(url, json, token);
        // 使用TypeReference指定Result<UserLoginVO>的类型
        Result<LinkCreateVO> result = JSONUtil.toBean(returnJson, new TypeReference<Result<LinkCreateVO>>() {}, true);
        return result.getData().getFullShortUrl();
    }


    private boolean checkLogin() throws ShortLinkException {
        String url ="http://" + shortLinkConfiguration.getAdminHost() + ":" + shortLinkConfiguration.getAdminPort() + "/api/short-link/admin/v1/user/check-login";
        String returnJson = HttpUtil.getData(url);
        // 使用TypeReference指定Result<UserLoginVO>的类型
        Result<Boolean> result = JSONUtil.toBean(returnJson, new TypeReference<Result<Boolean>>() {}, true);
        return BooleanUtil.isTrue(result.getData());
    }
}
